package application.controller;

import application.anno.*;
import application.messager.MessageReceiver;
import application.messager.Messager;
import application.net.ClientSocket;
import application.net.HttpOperation;
import application.popupWindow.TestPopup;
import application.windows.CustomizedDialog;
import application.windows.UserInterface;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.geometry.Bounds;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ToggleButton;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import lombok.SneakyThrows;
import okhttp3.*;

import java.io.IOException;

@MainActivity
@Layout("BaseMain.fxml")
@StageTitle("主窗口")
@IconName("main.jpg")
public class BaseMain extends UserInterface {

    public static final String TEST_ACTION = "test_action";

    public static final String TEST_FLAG = "test_flag";

    public static final String TEST_EXTRA = "test_extra";

    @FXML
    private Button stageJump, layoutJump, floatShow, dialogShow, send, listShow, tableShow, treeShow, netJump;

    @Injection
    private TipPopup.Tips tips;

    @Injection
    private ClientSocket clientSocket;

    @Injection
    private TestPopup testPopup;

    private MessageReceiver receiver;

    @Injection
    private HttpOperation httpOperation;

    @FXML
    private ToggleButton typeSwitch;

    @FXML
    protected void initialize() throws IllegalAccessException {
        super.initialize();
        if (typeSwitch.isSelected()) {
            typeSwitch.setText("对话窗显示");
        } else {
            typeSwitch.setText("悬浮窗显示");
        }
        // 负责窗口跳转
        stageJump.setOnMouseClicked(event -> {
            // 主界面跳转
            startUserInterface(BaseMain.this, NextLayout.class, true);
        });

        // 实现点击启动寄生布局
        layoutJump.setOnMouseClicked(event -> {
            // 跳转到寄生布局演示界面
            startUserInterface(BaseMain.this, ParasiticTest.class, true);
        });

        // 点击打开悬浮窗
        floatShow.setOnMouseClicked(event -> {
            try {
                // testPopup对象，由框架自动注入
                testPopup.init();
                // 显示悬浮窗，并且左上方的坐标是鼠标在全屏中的xy坐标
                testPopup.show(floatShow, event.getScreenX(), event.getScreenY());
            } catch (IOException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        });

        // 点击显示对话窗口
        dialogShow.setOnMouseClicked(event -> {
            // 实例化alert，没有使用自动注入，因为javaFX的界面，需要在Application线程中执行，尚未完善UI组件的自动注入操作。
            TestDialog.Alert alert = new TestDialog.Alert();
            // 显示自定义弹出窗
            alert.show();
        });

        // 点击向全局发送广播。
        send.setOnMouseClicked(event -> {
            // 获取单选开关的状态
            boolean selected = typeSwitch.isSelected();
            // 实例化消息传递者
            Messager messager = new Messager(TEST_ACTION);
            // 将单选开关的状态消息放入消息传递者中
            messager.put(TEST_FLAG, selected);
            // 将相关的其它信息放入消息传递者中
            messager.put(TEST_EXTRA, "广播测试消息。");
            // 发送消息
            sendMessage(messager);
        });

        // 定义状态开关的监听器
        typeSwitch.selectedProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue) {
                // 为true时，则将text改为 “对话框显示”
                typeSwitch.setText("对话框显示");
            } else {
                // 为false时，则将text改为“悬浮窗显示”
                typeSwitch.setText("悬浮窗显示");
            }
        });

        listShow.setOnMouseClicked(event -> {
            // 跳转到ListView显示窗口
            startUserInterface(BaseMain.this, ListDemo.class, true);
        });

        tableShow.setOnMouseClicked(event -> {
            startUserInterface(BaseMain.this, TableShow.class, true);
        });
    }

    @Override
    protected void initMessageReceiver() {
        super.initMessageReceiver();
        // 广播接收
        receiver = new MessageReceiver() {
            @SneakyThrows
            @Override
            @MessageActions(actions = {TEST_ACTION}) // 框架会找到此注解，并且将此注解汇中的，actions自动添加到receiver的Action容器中
            public boolean receive(Messager messager) {
                // 判断Messager的动作是否与此注册的动作相符
                if (TEST_ACTION.equals(messager.getAction())) {
                    // 拿出TEST_FLAG对应的在messager对象中的value值
                    boolean isDialog = messager.get(TEST_FLAG, false);
                    if (isDialog) {
                        // 拿出TEST_EXTRA在messager中对应的value
                        Object o = messager.get(TEST_EXTRA, null);
                        if (o != null) {
                            // 实例化自定义弹出窗
                            CustomizedDialog customizedDialog = new CustomizedDialog(String.valueOf(o), ButtonType.FINISH);
                            Stage stage = (Stage) customizedDialog.getDialogPane().getScene().getWindow();
                            stage.setTitle("广播消息");
                            // 显示自定义弹出窗
                            customizedDialog.show();
                        }
                    } else {
                        Object o = messager.get(TEST_EXTRA, null);
                        if (o != null) {
                            tips.init();
                            TipPopup tipPopup = tips.getController();
                            tipPopup.initParam(String.valueOf(o));
                            Bounds bounds = send.localToScreen(send.getLayoutBounds());
                            tips.show(send, bounds.getMaxX(), bounds.getMaxY());
                        }
                    }
                }
                return true;
            }
        };
    }
}
